const visitPrc = 600
const drugPrc = 400
const info = [
  [
    "product",
    "问诊订单",
    "药品订单",
    "今日收入",
    "今日问诊收入",
    "今日药品收入",
  ],
  ["03-01", 400, 230, 332000, 240000, 92000],
  ["03-02", 333, 222, 288600, 199800, 88800],
  ["03-03", 444, 333, 399600, 266400, 133200],
  ["03-04", 666, 555, 621600, 399600, 222000],
  ["03-05", 888, 777, 843600, 532800, 310800],
  ["03-06", 343, 234, 299400, 205800, 93600],
  ["03-07", 567, 456, 522600, 340200, 182400],
  ["03-08", 678, 567, 633600, 406800, 226800],
  ["03-09", 345, 234, 300600, 207000, 93600],
  ["03-10", 789, 689, 749000, 473400, 275600],
  ["03-11", 678, 578, 638000, 406800, 231200],
  ["03-12", 478, 378, 438000, 286800, 151200],
  ["03-13", 289, 189, 249000, 173400, 75600],
  ["03-14", 467, 367, 427000, 280200, 146800],
  ["03-15", 678, 567, 633600, 406800, 226800],
  ["03-16", 789, 568, 700600, 473400, 227200],
  ["03-17", 678, 578, 638000, 406800, 231200],
  ["03-18", 567, 456, 522600, 340200, 182400],
  ["03-19", 345, 245, 305000, 207000, 98000],
  ["03-20", 567, 467, 527000, 340200, 186800],
  ["03-21", 789, 689, 749000, 473400, 275600],
  ["03-22", 890, 767, 840800, 534000, 306800],
  ["03-23", 690, 590, 650000, 414000, 236000],
  ["03-24", 789, 689, 749000, 473400, 275600],
  ["03-25", 378, 278, 338000, 226800, 111200],
  ["03-26", 456, 256, 376000, 273600, 102400],
  ["03-27", 780, 680, 740000, 468000, 272000],
  ["03-28", 567, 456, 522600, 340200, 182400],
  ["03-29", 780, 678, 739200, 468000, 271200],
  ["03-30", 689, 568, 640600, 413400, 227200],
  ["03-31", 400, 333, 373200, 240000, 133200],
]
let intervalDay = 8 //日期间隔
let visitTotal = []
let drugTotal = []
// let visit = []
// let drug = []
let visitTotalSum = 0 //近段时间总问诊收入
let drugTotalSum = 0 //近段时间总订单收入
let visitSum = 0 //近段时间总问诊总订单
let drugSum = 0 //近段时间总药品总订单
//求和计算
function sliceDataSum(arr, num, index) {
  let sum = 0
  for (let i = 0; i < arr.slice(1, num).length; i++) {
    sum = sum + arr.slice(1, num)[i][index]
  }
  return sum
}
visitTotalSum = sliceDataSum(info, 8, 4)
drugTotalSum = sliceDataSum(info, 8, 5)
visitSum = sliceDataSum(info, 8, 1)
drugSum = sliceDataSum(info, 8, 2)
let infoL = info.slice(0, intervalDay)
let infoR = [
  ["name", "value"],
  ["问诊收入", visitTotalSum],
  ["药品收入", drugTotalSum],
]

//--------------折线统计图
const order_bar = echarts.init(document.getElementById("order-l-table"))
window.onresize = function () {
  order_bar.resize()
}

const optionL = {
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "cross",
      crossStyle: {
        color: "#999",
      },
    },
  },
  toolbox: {
    feature: {
      //   dataView: { show: true, readOnly: false },
      magicType: { show: true, type: ["line", "bar"] },
      restore: { show: true },
      saveAsImage: { show: true },
    },
  },
  legend: {
    // data: info[0],
  },
  dataset: {
    // 提供一份数据。
    source: infoL,
  },
  xAxis: [
    {
      type: "category",
      //   data: day,
      axisPointer: {
        type: "shadow",
      },
    },
  ],
  yAxis: [
    {
      type: "value",
      name: "订单",
      min: 0,
      max: 1200,
      splitNumber: 5,
      interval: 1200 / 5,
      axisLabel: {
        formatter: "{value}",
      },
    },
    {
      type: "value",
      name: "今日收入/￥",
      min: 0,
      max: 1000000,
      splitNumber: 5,
      interval: 1000000 / 5,
      axisLabel: {
        formatter: "{value}",
      },
    },
  ],
  series: [
    {
      name: info[0][1],
      type: "bar",
      tooltip: {
        valueFormatter: function (value) {
          return value + "单"
        },
      },
      //   data: visit,
      itemStyle: {
        normal: {
          label: {
            show: true, //开启显示
            position: "top", //在上方显示
            textStyle: {
              //数值样式
              color: "#5470c6",
              fontSize: 12,
            },
          },
        },
      },
    },
    {
      name: info[0][2],
      type: "bar",
      tooltip: {
        valueFormatter: function (value) {
          return value + "单"
        },
      },
      //   data: drug,
      itemStyle: {
        normal: {
          label: {
            show: true, //开启显示
            position: "top", //在上方显示
            textStyle: {
              //数值样式
              color: "#92cc76",
              fontSize: 12,
            },
          },
        },
      },
    },
    {
      name: info[0][3],
      type: "line",
      yAxisIndex: 1,
      tooltip: {
        valueFormatter: function (value) {
          return value + "￥"
        },
      },
      //   data: dayTotal,
      itemStyle: {
        normal: {
          label: {
            show: true, //开启显示
            position: "top", //在上方显示
            textStyle: {
              //数值样式
              color: "#fac858",
              fontSize: 12,
            },
          },
        },
      },
    },
  ],
  dataZoom: [
    //x轴拖拽
    {
      show: true,
      start: 40,
      end: 70,
    },
    //y轴拖拽
    {
      //   type: "inside",
      //   start: 94,
      //   end: 100,
    },
    {
      show: false,
      yAxisIndex: 0,
      filterMode: "empty",
      width: 30,
      height: "80%",
      showDataShadow: false,
      left: "93%",
    },
  ],
}
optionL && order_bar.setOption(optionL)

//饼状图

const order_r = echarts.init(document.getElementById("order-r-table"))
const optionR = {
  tooltip: {
    trigger: "item",
    formatter: "{a} <br/>{b}: {c} ({d}%)",
  },
  //   legend: {
  // data: [info[0][1], info[0][2]],
  //   },
  dataset: {
    // 提供一份数据。
    source: infoR,
    selected: 1,
  },
  series: [
    {
      name: "Access From",
      type: "pie",
      selectedMode: "single",
      radius: [0, "70%"],
      label: {
        position: "inner",
        fontSize: 14,
        formatter: "{c}" + "\n\r" + "({d}%)",
      },
      labelLine: {
        show: false,
      },
      encode: {
        itemName: "name",
        value: "value",
      },
      //   data: [
      //     { value: visitTotalSum, name: info[0][1] },
      //     { value: drugTotalSum, name: info[0][2], selected: true },
      //   ],
    },
  ],
}
optionR && order_r.setOption(optionR)

//jq  修改数据
$(".date-tab button").on("click", function () {
  $(".date-tab button").removeClass("aActive")
  $(this).addClass("aActive")
  // 渲染左侧表格
  intervalDay = $(this).attr("data-date")
  infoL = info.slice(0, intervalDay)
  optionL.dataset.source = infoL
  order_bar.setOption(optionL)
  //渲染右侧表格
  visitTotalSum = sliceDataSum(info, intervalDay, 4)
  drugTotalSum = sliceDataSum(info, intervalDay, 5)
  infoR[1][1] = visitTotalSum
  infoR[2][1] = drugTotalSum
  optionR.dataset.source = infoR
  order_r.setOption(optionR)

  //更新四个item
  visitSum = sliceDataSum(info, intervalDay, 1)
  drugSum = sliceDataSum(info, intervalDay, 2)
  $("#p-item1 .p-item-t-r h4")
    .eq(0)
    .text(`近${intervalDay - 1}日问诊订单数`)
  $("#p-item1 .p-item-t-r h4").eq(1).text(`${visitSum}`)
  $("#p-item2 .p-item-t-r h4")
    .eq(0)
    .text(`近${intervalDay - 1}日药品订单数`)
  $("#p-item2 .p-item-t-r h4").eq(1).text(`${drugSum}`)
  $("#p-item3 .p-item-t-r h4")
    .eq(0)
    .text(`近${intervalDay - 1}日药品收益`)
  $("#p-item3 .p-item-t-r h4").eq(1).text(`${drugTotalSum}￥`)
  $("#p-item3 .platform-item-b")
    .eq(0)
    .text(`近七日总收益: ${visitTotalSum + drugTotalSum}￥`)
})
//渲染四个item
$("#p-item1 .p-item-t-r h4").eq(0).text("近7日问诊订单数")
$("#p-item1 .p-item-t-r h4").eq(1).text(`${visitSum}`)
$("#p-item1 .platform-item-b").eq(0).text(`今日订单数:${info[1][1]}`)
$("#p-item2 .p-item-t-r h4").eq(0).text("近7日药品订单")
$("#p-item2 .p-item-t-r h4").eq(1).text(`${drugSum}`)
$("#p-item2 .platform-item-b").eq(0).text(`今日药品订单${info[1][2]}`)
$("#p-item3 .p-item-t-r h4").eq(0).text("近7日药品收益")
$("#p-item3 .p-item-t-r h4").eq(1).text(`${drugTotalSum}￥`)
$("#p-item3 .platform-item-b")
  .eq(0)
  .text(`近七日总收益: ${visitTotalSum + drugTotalSum}￥`)
